$(function() {
	// resetImgSrc();
	$('#memo').val($.cookie(cookiename));
    $('#memo').on('keydown',function(){
        var memo_str = $('#memo').val();
        $.cookie(cookiename,memo_str,{expires:31});
    });
    $('#memo').on('blur',function(){
        var memo_str = $('#memo').val();
        $.cookie(cookiename,memo_str,{expires:31});
    });
	// 	公告主题颜色
	$('.s-skin-0').on('click',function(){
		var oTitle = $(".J_iframe").contents().find(".layui-colla-title");
		var oTitleTime = $(".J_iframe").contents().find(".span-info.span-strong");
		oTitle.css({'background-color':'#2f4050','color':'#a7b1c2'});
		oTitleTime.css({'color':'#5bc0de'});
	});
	$('.s-skin-1').on('click',function(){
		var oTitle = $(".J_iframe").contents().find(".layui-colla-title");
		var oTitleTime = $(".J_iframe").contents().find(".span-info.span-strong");
		oTitle.css({'background-color':'#39aef5','color':'#FFF'});
		oTitleTime.css({'color':'#FFF'});
	})
	$('.s-skin-3').on('click',function(){
		var oTitle = $(".J_iframe").contents().find(".layui-colla-title");
		var oTitleTime = $(".J_iframe").contents().find(".span-info.span-strong");
		oTitle.css({'background-color':'#3e2c42','color':'#a7b1c2'});
		oTitleTime.css({'color':'#ecba52'});
	})
	// 消息列表
	$("#msg-list-btn").on('click',function(){
		$("#msg-list").toggleClass("not-show");
		if($("#msg-list")[0].className == "not-show" && $("#right-sidebar")[0].className == ""){
			$(".Mask").hide();
		}else{
			$(".Mask").show();
		}
	})
	$(".Mask").on("click",function() {
		$("#msg-list").addClass("not-show");
		$("#right-sidebar").removeClass("sidebar-open");
		$(".Mask").hide();
	})
	$(".right-sidebar-toggle").on("click",function() {
		if($("#msg-list")[0].className == "not-show" && $("#right-sidebar")[0].className == ""){
			$(".Mask").hide();
		}else{
			$(".Mask").show();
		}
	})
	$("#msg-list .hide_mask").on("click",function() {
		$("#msg-list-btn").click();
	});
	$(".dropdown-menu li .hide_mask").on("click",function() {
		$(".right-sidebar-toggle").click();
	})
	setMsgListStatus(0);
})

// reset img src
function resetImgSrc() {
	$("img").on("error",function() {
		var width = $(this)[0].offsetWidth;
		var height = $(this)[0].offsetHeight;
		if(width <= 65 && height <= 65){
			$(this).attr({'src':"__IMG__/user.png"});
		}else{
			$(this).attr({'src':"__IMG__/img_404.png"});
		}
	})
}

function setMsgListStatus(data) {
	var List = $(".pointer"),showBtnDot = false;
	for(var i=0;i<List.length;i++){
		var msg_id = List[i].getAttribute("data-msg_id");
		if(data != 0){
			if(data.info.chat_room_id == msg_id){
				List[i].setAttribute("data-is_read",0);
				List[i].querySelector(".last-msg").innerHTML = data.msg;
				var user_head = List[i].querySelector("img");
				user_head.src = data.info.head;
				user_head.title = data.info.chat_room_name + ":" + data.info.real_name;
			}
		}
		var is_read = List[i].getAttribute("data-is_read");
		if(is_read == 0){
			showBtnDot = true;
			List[i].querySelector(".layui-badge-dot").className = "layui-badge-dot";
		}else{
			List[i].querySelector(".layui-badge-dot").className = "layui-badge-dot not-show";
		}
	}
	if(showBtnDot){
		$("#msgCount").show();
	}else{
		$("#msgCount").hide();
	}
}
